//
//  FANHomeCell.m
//  Weibo - OC
//
//  Created by user on 16/4/21.
//  Copyright © 2016年 user. All rights reserved.
//

#import "FANHomeCell.h"
#import "FANOriginalView.h"
#import "FANToolBar.h"
#import "FANRetweetView.h"

//#warning 宏定义一定要放到 导入头文件的 上面, 会影响编译

//define this constant if you want to use Masonry without the 'mas_' prefix

#define MAS_SHORTHAND

//define this constant if you want to enable auto-boxing for default syntax

#define MAS_SHORTHAND_GLOBALS

#import <Masonry/Masonry.h>
#import <Masonry/MASConstraint.h>

#define kMargin 10

@interface FANHomeCell ()

@property (nonatomic, strong) FANOriginalView *originalView;

@property (nonatomic, strong) FANToolBar *toolBar;

@property (nonatomic, strong) FANRetweetView *retweetView;

@property (nonatomic, strong) MASConstraint *toolBarCons;

@end

@implementation FANHomeCell

- (FANRetweetView *)retweetView{
    
    if (!_retweetView) {
        
        _retweetView = [[FANRetweetView alloc] init];
    }
    
    return _retweetView;
}

- (FANToolBar *)toolBar{
    
    if (!_toolBar) {
        
        _toolBar = [[FANToolBar alloc] init];
    }
    
    return _toolBar;
}

- (FANOriginalView *)originalView{
    
    if (!_originalView) {
        
        _originalView = [[FANOriginalView alloc] init];
    }
    
    return _originalView;
}

- (void)setStatusViewModel:(FANStatusViewModel *)statusViewModel{
    
    _statusViewModel = statusViewModel;
    
    self.originalView.statusViewModel = statusViewModel;
    
    [self.toolBarCons uninstall];
    
    if (statusViewModel.status.retweeted_status != nil) {
        
        self.retweetView.statusViewModel = statusViewModel;
        
        self.retweetView.hidden = false;
        
        [self.toolBar updateConstraints:^(MASConstraintMaker *make) {
            
            self.toolBarCons = make.top.equalTo(self.retweetView.bottom);
        }];
        
    }else{
        
        self.retweetView.hidden = true;
        
        [self.toolBar updateConstraints:^(MASConstraintMaker *make) {
           
            self.toolBarCons = make.top.equalTo(self.originalView.bottom);
        }];
    }
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if ( self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self setupUI];
    }
    
    return self;
}

- (void)setupUI{
    
    self.backgroundColor = [UIColor colorWithWhite:220/255.0 alpha:1];
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    [self.contentView addSubview:self.originalView];
    
    [self.contentView addSubview:self.toolBar];
    
    [self.contentView addSubview:self.retweetView];
    
    [self.originalView makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.contentView).offset(kMargin);
        
        make.trailing.leading.equalTo(self.contentView);
        
//        make.height.equalTo(150);
        
    }];
    
    
    [self.retweetView makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.originalView.bottom);
        
        make.leading.trailing.equalTo(self.contentView);
    }];
    
    [self.toolBar makeConstraints:^(MASConstraintMaker *make) {
        
//        make.bottom.equalTo(self.contentView);
        
       self.toolBarCons = make.top.equalTo(self.originalView.bottom);
        
        make.trailing.leading.equalTo(self.contentView);
        
    }];
    
    [self.contentView makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(self.toolBar.bottom).offset(FANMargin);
        
        make.leading.top.trailing.equalTo(self);
        
    }];
    
}

@end
